package com.hmsproject.common;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.hmsproject.datastructures.Hotel;
import com.hmsproject.datastructures.User;
import com.hmsproject.datastructures.event.Event;
import com.hmsproject.datastructures.mail.HMSMail;
import com.hmsproject.datastructures.transactions.Transaction;
import com.hmsproject.db.inmemorydb.HotelMap;
import com.hmsproject.db.inmemorydb.TransactionMap;
import com.hmsproject.db.inmemorydb.UserMap;
import com.hmsproject.livedata.LiveDealCriterion;
import com.hmsproject.livedata.LiveDealData;

public class MailUtil {
	
	public static final Format formatter = new SimpleDateFormat("MM-dd-yyyy");
	
	public static void sendHotellieMails(Transaction newTransacion,LiveDealData freshLiveDealData, User user){
		
		try{
			String subject = "Congralutions from Hotellie, your booking is confirmed for " + newTransacion.getDeal().getDealName();
			
			StringBuilder messageBody = new StringBuilder();
			
			messageBody.append("Hello " + user.getFirstName() + " " + user.getLastName() + ",\n\n");
			messageBody.append("  " + subject + "\n");
			messageBody.append("  Please find below the booking Details \n\n");

			messageBody.append("  Checkin Date: " + formatter.format(freshLiveDealData.getFromDate()) + "\n");
			messageBody.append("  Checkout Date: " + formatter.format(freshLiveDealData.getToDate()) + "\n");
			Hotel hotel = HotelMap.getInstance().getHotel(newTransacion.getDeal().getHotelId());
			messageBody.append("  Hotel Name, location: " + hotel.getName() + " , " + hotel.getPlaceName() + "\n");
			messageBody.append("  Booked on: " + formatter.format(newTransacion.getTransactionDate()) + "\n");
			messageBody.append("  No of rooms booked: " + freshLiveDealData.getNoOfRooms() + "\n");
			messageBody.append("  Total Cost: $" + freshLiveDealData.getTotalCost() + "\n\n\n\n");

			List<LiveDealCriterion> liveDealCriterions = freshLiveDealData.getLiveDealCriterions();
			
			if(liveDealCriterions.size() > 0)
			messageBody.append("  Further Offers:\n\n");
			for(int i = 0; i < liveDealCriterions.size(); i++){
				LiveDealCriterion tmpLiveDealCriterion = liveDealCriterions.get(i);
				messageBody.append("  " + tmpLiveDealCriterion.getNoOfRoomsMore() + " more to get " + tmpLiveDealCriterion.getAdditionalPercentOff()+" percent Off\n");
			}
			
			messageBody.append("\n");
			messageBody.append("Please refer this deal to your friends to get the above mentioned discounts. \n");
			messageBody.append("  " + freshLiveDealData.getDealUrl());
			messageBody.append("\n\n");
			messageBody.append("Thanks\n");
			messageBody.append("Hotellie Admin");

			
			HMSMail hmsMail = new HMSMail(user.getEmailAddress(), messageBody.toString(), subject);
			
			sendMailProper(hmsMail);
		}catch(Exception e){
			e.printStackTrace();
		}		
		
	}	
	
	public static void sendNotificationMails(Transaction newTransacion,LiveDealData freshLiveDealData,User user){
		
		try{
			String subject = "Hotellie Notification: A new user has bought the deal " + newTransacion.getDeal().getDealName();
			
			StringBuilder messageBody = null;
		
			List<Transaction> transactionList = TransactionMap.INSTANCE.getTransactionListForDeal(freshLiveDealData.getDeal().getDealId());
			
			for(int k = 0 ; k < transactionList.size(); k++)
			{
				
				messageBody = new StringBuilder();
				Transaction oldTransaction = transactionList.get(k);
				User olduser = UserMap.getInstance().getUser(oldTransaction.getUserId());
				messageBody.append("Hello " + olduser.getFirstName() + " " + olduser.getLastName() + ",\n\n");
				messageBody.append("  " + subject + "\n");
				messageBody.append("  Please find below the booking Details \n\n");
				messageBody.append("  User Name: "  + user.getFirstName() + " " + user.getLastName() +"\n");
				messageBody.append("  Checkin Date: " + formatter.format(freshLiveDealData.getFromDate()) + "\n");
				messageBody.append("  Checkout Date: " + formatter.format(freshLiveDealData.getToDate()) + "\n");
				Hotel hotel = HotelMap.getInstance().getHotel(newTransacion.getDeal().getHotelId());
				messageBody.append("  Hotel Name, location: " + hotel.getName() + " , " + hotel.getPlaceName() + "\n");
				messageBody.append("  Booked on: " + formatter.format(newTransacion.getTransactionDate()) + "\n");
				messageBody.append("  Total Cost: $" + freshLiveDealData.getTotalCost() + "\n\n\n\n");
	
				List<LiveDealCriterion> liveDealCriterions = freshLiveDealData.getLiveDealCriterions();
				
				if(liveDealCriterions.size() > 0)
				messageBody.append("  Further Offers:\n\n");
				for(int i = 0; i < liveDealCriterions.size(); i++){
					LiveDealCriterion tmpLiveDealCriterion = liveDealCriterions.get(i);
					messageBody.append("  " + tmpLiveDealCriterion.getNoOfRoomsMore() + " more to get " + tmpLiveDealCriterion.getAdditionalPercentOff()+" percent Off\n");
				}
				
				messageBody.append("\n");
				messageBody.append("Please refer this deal to your friends to get the above mentioned discounts. \n");
				messageBody.append("  " + freshLiveDealData.getDealUrl());
				messageBody.append("\n\n");
				messageBody.append("Thanks\n");
				messageBody.append("Hotellie Admin");
	
				
				HMSMail hmsMail = new HMSMail(olduser.getEmailAddress(), messageBody.toString(), subject);
				
				sendMailProper(hmsMail);
			}
		}catch(Exception e){
			e.printStackTrace();
		}		
		
	}		
	
	public static void sendMailProper(HMSMail hmsMail){
		
	    String host = "smtp.gmail.com";
	    String port = "587";

	    Properties properties = System.getProperties();
	    properties.setProperty("mail.smtp.host", host);
	    properties.setProperty("mail.smtp.port", port);
	    properties.setProperty("mail.smtp.starttls.enable", "true");

	    Session session = Session.getDefaultInstance(properties);
	
	    try{
	         MimeMessage message = new MimeMessage(session);
	         message.setFrom(new InternetAddress(hmsMail.getFromAddress()));
	         message.addRecipient(Message.RecipientType.TO,
	                                  new InternetAddress(hmsMail.getToAddress()));

	         message.setSubject(hmsMail.getSubject());
	         message.setText(hmsMail.getMessageBody());
	         Transport tr = session.getTransport("smtp");
	         tr.connect(host, "hmsuser441@gmail.com", "hmsuser123");
	         message.saveChanges();
	         tr.sendMessage(message, message.getAllRecipients());
	         tr.close();

	      }catch (Exception mex) {
	         mex.printStackTrace();
	      }	    
	    
		
	}	

}
